package com.hmsproject.search.managedbean;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.hmsproject.common.HMSManagedBeans;
import com.hmsproject.db.util.HMSDBUtil;


@ManagedBean
@SessionScoped
public class ChangePasswordView {
	
	private String currentPassword;
	private String newPassword;
	private String repeatPassword;
	
	
	public String getCurrentPassword() {
		return currentPassword;
	}
	public void setCurrentPassword(String currentPassword) {
		this.currentPassword = currentPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getRepeatPassword() {
		return repeatPassword;
	}
	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}
	
    public String changePassword(){
    	String email = HMSManagedBeans.getHMSManagedBeans().getEmailAddress();
    	String password = HMSManagedBeans.getHMSManagedBeans().getPassword();
    	
    	System.out.println("PASSWORD:   " + password);
    	System.out.println("currentPassword:   " + currentPassword);
    	
        if(! password.equals(this.currentPassword)){
        	FacesContext.getCurrentInstance().addMessage(null, 
			        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect current password, please check.", null));	
			return null;
        }
        
        if(! this.newPassword.equals(this.repeatPassword)){
        	FacesContext.getCurrentInstance().addMessage(null, 
			        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect repeat password, please check.", null));	
			return null;
        } 
    	
    	boolean result = HMSDBUtil.getInstance().changePassword(email, this.newPassword);
    	
    	FacesContext.getCurrentInstance().addMessage(null, 
		        new FacesMessage(FacesMessage.SEVERITY_INFO, "Password changed successfully", null));
    	
		return "/user/signIn.xhtml??faces-redirect=true";
    }

}
